{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "30b400f1",
   "metadata": {},
   "source": [
    "### AI学习"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b21d7287",
   "metadata": {},
   "source": [
    "#### 常用包导入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "0e0628f9-a3cd-450b-8aed-1f36dfd56767",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np # 科学计算库\n",
    "import matplotlib.pyplot as plt # 绘图库\n",
    "import pandas as pd # 数据处理库\n",
    "import scipy as sp # 科学计算库\n",
    "\n",
    "from sklearn.linear_model import LinearRegression # 线性回归\n",
    "from sklearn.neighbors import KNeighborsClassifier # K近邻分类\n",
    "\n",
    "from sklearn.model_selection import train_test_split # 训练集测试分隔\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e5f6c7a",
   "metadata": {},
   "source": [
    "#### 数据归一化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c26b7e99",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import MinMaxScaler # 最小-最大标准化，将数据缩放到0和1之间\n",
    "from sklearn.preprocessing import StandardScaler # 标准化，将数据缩放到均值为0，标准差为1的分布\n",
    "from sklearn.preprocessing import MaxAbsScaler # 最大绝对值标准化，将数据缩放到-1和1之间\n",
    "from sklearn.preprocessing import RobustScaler # 鲁棒标准化，将数据缩放到中位数和四分位数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d18b7432",
   "metadata": {},
   "source": [
    "#### 模型评估"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6ae816f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics import accuracy_score # 准确率 正确分类样本的比例\n",
    "from sklearn.metrics import precision_score # 精确率 针对预测为正类别的样本中真正为正类别的比例\n",
    "from sklearn.metrics import recall_score # 召回率 实际为正类别的样本中被正确预测的比例\n",
    "from sklearn.metrics import f1_score # F1值 精确率和召回率的调和平均数，综合考虑了两者\n",
    "from sklearn.metrics import roc_auc_score,roc_curve # Receiver Operating Characteristic曲线和Area Under Curve值用于评估模型的分类阈值敏感性\n",
    "from sklearn.metrics import log_loss # 负对数似然值\n",
    "from sklearn.metrics import mean_squared_error # 均方误差 预测值与真实值之差平方的平均值\n",
    "from sklearn.metrics import mean_absolute_error # 平均绝对误差 预测值与真实值之差的绝对值的平均值\n",
    "from sklearn.metrics import r2_score # R2值 衡量模型拟合优度，值越接近1表示模型越好\n",
    "from sklearn.metrics import explained_variance_score # 预测值与真实值之差平方和除以真实值之差平方和\n",
    "from sklearn.metrics import mean_squared_log_error # 均方对数误差 预测\n",
    "from sklearn.model_selection import cross_val_score # 交叉验证 为了得到更稳健的评估结果，可以使用交叉验证策略来评估模型性能\n",
    "from sklearn.model_selection import cross_val_predict # 交叉验证预测\n",
    "from sklearn.model_selection import GridSearchCV #最优超参数，并同时评估不同组合下的模型性能\n",
    "from sklearn.model_selection import RandomizedSearchCV #最优超参数，并同时评估不同组合下的模型性能"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe442b7d",
   "metadata": {},
   "source": [
    "#### sk数据集导包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "01e3c974-838a-4805-84d5-999eaa416115",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sklearn.datasets as datasets # 开源数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "ce3a856c-9f71-4a05-baf1-9b5a64906380",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.22222222, 0.625     , 0.06779661, 0.04166667],\n",
       "       [0.16666667, 0.41666667, 0.06779661, 0.04166667],\n",
       "       [0.11111111, 0.5       , 0.05084746, 0.04166667],\n",
       "       [0.08333333, 0.45833333, 0.08474576, 0.04166667],\n",
       "       [0.19444444, 0.66666667, 0.06779661, 0.04166667]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(120, 4)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(30, 4)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(120,)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(30,)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 导入数据，数据分为测试和训练数据\n",
    "X, y = datasets.load_iris(return_X_y=True) #导入数据\n",
    "X = MinMaxScaler().fit_transform(X)\n",
    "display(X[:5])\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1024)\n",
    "display(X_train.shape, X_test.shape, y_train.shape, y_test.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "adce94e8-d14f-4c16-8095-824b49b32d58",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9666666666666667"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "model = KNeighborsClassifier(n_neighbors=5)\n",
    "model.fit(X_train, y_train)\n",
    "display(model.score(X_test, y_test))\n",
    "y_pred = model.predict(X_test)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "409a70a2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 0, 2, 2, 0, 0, 1, 2, 1, 0, 0, 0, 1, 2, 0, 0, 1, 0, 2, 0, 2, 0,\n",
       "       1, 0, 2, 1, 2, 2, 2, 2])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(y_pred)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e19543ad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 0, 2, 1, 1, 0, 1, 2, 1, 1, 2, 0, 0, 0, 0, 1, 2, 1, 1, 2, 0, 2,\n",
       "       0, 2, 2, 2, 2, 2, 0, 0, 0, 0, 1, 0, 0, 2, 1, 0, 0, 0, 2, 1, 1, 0,\n",
       "       0, 1, 2, 2, 1, 2])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(y_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3401fd16",
   "metadata": {},
   "source": [
    "### 数据归一化"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28ea8a31",
   "metadata": {},
   "source": [
    "#### normalize\n",
    "\n",
    "\n",
    "L1:  $\\Large{\\frac{x_i}{|x_1| +| x_2| + ... + |x_n|}}$  \n",
    "L2:$\\Large{\\frac{x_i}{\\sqrt[2]{x_1^2 + x_2^2 + ... + x_n^2}}}$\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "591b56c4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.22, 0.29],\n",
       "       [1.  , 0.88],\n",
       "       [0.44, 0.96],\n",
       "       [0.78, 0.64],\n",
       "       [0.78, 0.51],\n",
       "       [0.56, 0.77],\n",
       "       [0.67, 0.45],\n",
       "       [0.67, 0.58],\n",
       "       [0.89, 0.73],\n",
       "       [0.22, 1.  ]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn import preprocessing\n",
    "import numpy as np\n",
    "x1 = np.random.randint(1,10,10)\n",
    "x2 = np.random.randint(1000,5000,10)\n",
    "x_merge = np.c_[x1,x2]\n",
    "X = preprocessing.normalize(x_merge,norm='max',axis=0)\n",
    "np.around(X,2)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34c292f0",
   "metadata": {},
   "source": [
    "#### scale "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "b9b1f855",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1.42,  1.41],\n",
       "       [ 1.2 ,  0.98],\n",
       "       [ 0.45, -1.28],\n",
       "       [ 0.07,  0.08],\n",
       "       [-1.05,  0.48],\n",
       "       [-0.3 , -1.09],\n",
       "       [ 1.57, -0.36],\n",
       "       [ 0.82,  1.52],\n",
       "       [ 0.07, -0.68],\n",
       "       [-1.42, -1.05]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn import preprocessing\n",
    "import numpy as np\n",
    "x1 = np.random.randint(1,10,10)\n",
    "x2 = np.random.randint(1000,5000,10)\n",
    "x_merge = np.c_[x1,x2]\n",
    "X = preprocessing.scale(x_merge,axis=0) #with_mean: bool = True,方差为1，with_std: bool = True,均值为0\n",
    "np.around(X,2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5788825",
   "metadata": {},
   "source": [
    "### min-max缩放\n",
    "\n",
    "$\\Large{\\frac{x - x_{min}}{x_{max} - x_{min}}}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "863a7c94",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.5 , 0.08],\n",
       "       [1.  , 0.73],\n",
       "       [1.  , 0.  ],\n",
       "       [0.62, 0.43],\n",
       "       [0.  , 0.56],\n",
       "       [0.38, 1.  ],\n",
       "       [0.25, 0.9 ],\n",
       "       [0.25, 0.29],\n",
       "       [0.75, 0.12],\n",
       "       [0.5 , 0.98]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn import preprocessing\n",
    "import numpy as np\n",
    "x1 = np.random.randint(1,10,10)\n",
    "x2 = np.random.randint(1000,5000,10)\n",
    "x_merge = np.c_[x1,x2]\n",
    "X = preprocessing.minmax_scale(x_merge,axis=0)\n",
    "np.around(X,2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "294e1a48",
   "metadata": {},
   "source": [
    "### stadndard 均值为0,方差为1\n",
    "\n",
    "$\\Large{\\frac{x -\\mu}{\\delta}}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "645ace42",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.17,  0.86],\n",
       "       [ 0.78, -1.41],\n",
       "       [-0.39,  1.  ],\n",
       "       [-0.39, -0.22],\n",
       "       [-1.56, -0.11],\n",
       "       [-0.39, -2.11],\n",
       "       [-1.56,  0.72],\n",
       "       [ 0.39,  0.23],\n",
       "       [ 0.39, -0.05],\n",
       "       [ 1.56,  1.09]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn import preprocessing\n",
    "import numpy as np\n",
    "x1 = np.random.randint(1,10,10)\n",
    "x2 = np.random.randint(1000,5000,10)\n",
    "x_merge = np.c_[x1,x2]\n",
    "psd = preprocessing.StandardScaler()\n",
    "X = psd.fit_transform(x_merge)\n",
    "np.around(X,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "57e341ae",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
